//
//  AddViewController.m
//  18-SQLiteHomeWork
//
//  Created by 曹曹 on 16/8/8.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "AddViewController.h"
#import "Student.h"
#import "SQLiteDataBaseManager.h"

@interface AddViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *address;
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (nonatomic, strong) NSData *imageData;

@end

@implementation AddViewController

- (IBAction)addStudent:(id)sender {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *avatarDirectory = [[pathArray firstObject] stringByAppendingPathComponent:@"avatarsDirectory"];
    // manager
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:avatarDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    // default image
    UIImage *defaultAvatar = [UIImage imageNamed:@"0"];
    NSData *defaultData = UIImagePNGRepresentation(defaultAvatar);
    NSString *defaultPath = [avatarDirectory stringByAppendingPathComponent:@"default.png"];
    [defaultData writeToFile:defaultPath atomically:YES];
    // stu
    Student *stu = [[Student alloc] init];
    stu.stu_name = _name.text;
    stu.stu_phone = _phone.text;
    stu.stu_address = _address.text;
    // 
    NSString *avatarPath = [avatarDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", _name.text]];
    NSLog(@"add-----%@", avatarPath);
    [_imageData writeToFile:avatarPath atomically:YES];
    stu.stu_avatar = avatarPath;
    if (self.delegate && [self.delegate respondsToSelector:@selector(addStudent:)]) {
        [self.delegate addStudent:stu];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setDelegate:(id<AddViewControllerDelegate>)delegate {
    _delegate = delegate;
}
- (IBAction)chooseImage:(id)sender {
    [self getImageFromIpc];
}
- (void)getImageFromIpc
{
    // 1.判断相册是否可以打开
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
    // 2. 创建图片选择控制器
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    // 3. 设置打开照片相册类型(显示所有相簿)
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    ipc.delegate = self;
    // 5.modal出这个控制器
    [self presentViewController:ipc animated:YES completion:nil];
}

#pragma mark -- <UIImagePickerControllerDelegate>--
// 获取图片后的操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    // 销毁控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    // 设置图片
    self.avatar.image = info[UIImagePickerControllerOriginalImage];
    NSData *data;
    if (UIImagePNGRepresentation(self.avatar.image) == nil) {
        data = UIImageJPEGRepresentation(self.avatar.image, 1);
    } else {
        data = UIImagePNGRepresentation(self.avatar.image);
    }
    self.imageData = [NSData data];
    _imageData = data;
}
- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
